package com.bci.pwtz.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.exception.CanNotDeleteException;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.CompanyMessageMapper;
import com.bci.pwtz.mysql.model.CompanyMessage;
import com.bci.pwtz.service.BusinessRefService;
import com.bci.pwtz.service.CompanyMessageService;
@Service
public class CompanyMessageServiceImpl implements CompanyMessageService
{
    private CompanyMessageMapper companyMessageMapper;
    private BusinessRefService businessRefService;
    private static final Logger _logger=Logger.getLogger(CompanyMessageServiceImpl.class);    
    public CompanyMessageMapper getCompanyMessageMapper()
    {
        return companyMessageMapper;
    }

    public void setCompanyMessageMapper(CompanyMessageMapper companyMessageMapper)
    {
        this.companyMessageMapper = companyMessageMapper;
    }


    public BusinessRefService getBusinessRefService()
    {
        return businessRefService;
    }

    public void setBusinessRefService(BusinessRefService businessRefService)
    {
        this.businessRefService = businessRefService;
    }

    @Override
    public CompanyMessage add(CompanyMessage companyMessage) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("add:companyMessage"+companyMessage.toString());
        }
        if(companyMessage==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("add,companyMessage is null");
            }
            return null;
        }else{
            companyMessage.setCreateTime(new Date());
            companyMessage.setReadStatus(1);
            companyMessage.setAppStatus(1);
        }
        try{
            companyMessageMapper.insertSelective(companyMessage);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return companyMessage;
    }

    @Override
    public int delete(Long messageid) throws PwtzDatabaseException,CanNotDeleteException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("delete:messageid="+messageid);
        }
        if(messageid==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("delete,messageid is null");
            }
            return 0;
        }
        int result=0;
        try{
            boolean bl=businessRefService.canDelete("T_COMPANY_MESSAGE", messageid.toString());
            if(!bl){
                throw new CanNotDeleteException();
            }
            result=companyMessageMapper.deleteByPrimaryKey(messageid);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public int update(CompanyMessage companyMessage) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("update:companyMessage="+companyMessage.toString());
        }
        if(companyMessage==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("update,companyMessage is null");
            }
            return 0;
        }
        int result=0;
        try{
            result=companyMessageMapper.updateByPrimaryKeySelective(companyMessage);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public List<CompanyMessage> findCompanyMessageList(Long userid) throws PwtzDatabaseException
    {
        if(_logger.isDebugEnabled()){
            _logger.debug("findCompanyMessage:userid="+userid);
        }
        if(userid==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("findCompanyMessage,userid is null");
            }
            return null;
        }
        List<CompanyMessage> list=null;
        try{
            list=companyMessageMapper.selectCompanyMessages(userid);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if(list!=null){
            _logger.debug("findCompanyMessage:result list="+list.toString());
        }
        return list;
    }

    @Override
    public PaginationList<CompanyMessage> selectCompanyMessages(int pageSize, int pageNo,String companyName,long userId) throws PwtzDatabaseException
    {
        PaginationList<CompanyMessage> paginationList = new PaginationList<CompanyMessage>();
        
        List<CompanyMessage> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = companyMessageMapper.selectCompanyMessage(benginIndex, pageSize,companyName,userId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("selectAllCompanyMessages:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = companyMessageMapper.selectCountCompanyMessages(companyName,userId);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
    
    @Override
    public CompanyMessage selectCompanyMessageById(Long messageId) throws PwtzDatabaseException
    {
    	CompanyMessage companyMessage = new CompanyMessage();
        if(messageId==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("selectCompanyMessageById,messageId is null");
            }
            return null;
        }
        try{
        	companyMessage = companyMessageMapper.selectByPrimaryKey(messageId);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return companyMessage;
    }
}
